<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Config_Writer - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.config.writer.introduction.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.config.writer.introduction.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.config.writer.html">Zend_Config_Writer</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.config.writer.html">Zend_Config_Writer</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.console.getopt.html">Zend_Console_Getopt</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.config.writer.introduction" class="section"><div class="info"><h1 class="title">Zend_Config_Writer</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Config_Writer</span> gives you the ability to write config
        files out of <span class="classname">Zend_Config</span> objects. It works with an
        adapter-less system and thus is very easy to use. By default
        <span class="classname">Zend_Config_Writer</span> ships with four adapters, which are all
        file-based. You instantiate a writer with specific options, which
        can be <em class="emphasis">filename</em> and <em class="emphasis">config</em>. Then
        you call the  <span class="methodname">write()</span> method of the writer and the config
        file is created. You can also give <var class="varname">$filename</var> and
        <var class="varname">$config</var> directly to the  <span class="methodname">write()</span> method.
        Currently the following writers are shipped with
        <span class="classname">Zend_Config_Writer</span>:
    </p>

    <ul class="itemizedlist">
        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Config_Writer_Array</span>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Config_Writer_Ini</span>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Config_Writer_Json</span>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Config_Writer_Xml</span>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <span class="classname">Zend_Config_Writer_Yaml</span>
            </p>
        </li>
    </ul>


    <p class="para">
        When modifying or creating a <span class="classname">Zend_Config</span> object, there are
        some things to know. To create or modify a value, you simply say set
        the parameter of the <span class="classname">Zend_Config</span> object via the parameter
        accessor (<em class="emphasis">-&gt;</em>). To create a section in the root or to
        create a branch, you just create a new array
        (&quot;<strong class="command">$config-&gt;branch = array();</strong>&quot;). To define which section
        extends another one, you call the  <span class="methodname">setExtend()</span> method
        on the root <span class="classname">Zend_Config</span> object.
    </p>

    <div class="example" id="zend.config.writer.example.using"><div class="info"><p><b>Example #1 Using Zend_Config_Writer</b></p></div>
        

        <div class="example-contents"><p>
            This example illustrates the basic use of
            <span class="classname">Zend_Config_Writer_Xml</span> to create a new config file:
        </p></div>

        <pre class="programlisting brush: php">
// Create the config object
$config = new Zend_Config(array(), true);
$config-&gt;production = array();
$config-&gt;staging    = array();

$config-&gt;setExtend(&#039;staging&#039;, &#039;production&#039;);

$config-&gt;production-&gt;db = array();
$config-&gt;production-&gt;db-&gt;hostname = &#039;localhost&#039;;
$config-&gt;production-&gt;db-&gt;username = &#039;production&#039;;

$config-&gt;staging-&gt;db = array();
$config-&gt;staging-&gt;db-&gt;username = &#039;staging&#039;;

// Write the config file in one of the following ways:
// a)
$writer = new Zend_Config_Writer_Xml(array(&#039;config&#039;   =&gt; $config,
                                           &#039;filename&#039; =&gt; &#039;config.xml&#039;));
$writer-&gt;write();

// b)
$writer = new Zend_Config_Writer_Xml();
$writer-&gt;setConfig($config)
       -&gt;setFilename(&#039;config.xml&#039;)
       -&gt;write();

// c)
$writer = new Zend_Config_Writer_Xml();
$writer-&gt;write(&#039;config.xml&#039;, $config);
</pre>


        <div class="example-contents"><p>
            This will create an <acronym class="acronym">XML</acronym> config file with the sections production
            and staging, where staging extends production.
        </p></div>
    </div>

    <div class="example" id="zend.config.writer.modifying"><div class="info"><p><b>Example #2 Modifying an Existing Config</b></p></div>
        

        <div class="example-contents"><p>
            This example demonstrates how to edit an existing config file.
        </p></div>

        <pre class="programlisting brush: php">
// Load all sections from an existing config file, while skipping the extends.
$config = new Zend_Config_Ini(&#039;config.ini&#039;,
                              null,
                              array(&#039;skipExtends&#039;        =&gt; true,
                                    &#039;allowModifications&#039; =&gt; true));

// Modify a value
$config-&gt;production-&gt;hostname = &#039;foobar&#039;;

// Write the config file
$writer = new Zend_Config_Writer_Ini(array(&#039;config&#039;   =&gt; $config,
                                           &#039;filename&#039; =&gt; &#039;config.ini&#039;));
$writer-&gt;write();
</pre>

    </div>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Loading a Config File</b><br /></span>
        

        <p class="para">
            When loading an existing config file for modifications it is very
            important to load all sections and to skip the extends, so that
            no values are merged. This is done by giving the
            <em class="emphasis">skipExtends</em> as option to the constructor.
        </p>
    </p></blockquote>

    <p class="para">
        For all the File-Based writers (<acronym class="acronym">INI</acronym>, <acronym class="acronym">JSON</acronym>,
        <acronym class="acronym">XML</acronym>, <acronym class="acronym">YAML</acronym>, and <acronym class="acronym">PHP</acronym> Array)
        internally the  <span class="methodname">render()</span> is used to build the configuration string.
        This method can be used independently to access the string-representation of the
        configuration data.
    </p>

    <div class="section" id="zend.config.writer.introduction.ini-notes"><div class="info"><h1 class="title">Notes specific to the INI writer</h1></div>
        

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    The <acronym class="acronym">INI</acronym> writer has two modes for rendering with regard to
                    sections.  By default the top-level configuration is always written into section
                    names.  By calling <strong class="command">$writer-&gt;setRenderWithoutSections();</strong> all
                    options are written into the global namespace of the <acronym class="acronym">INI</acronym> file
                    and no sections are applied.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <span class="classname">Zend_Config_Writer_Ini</span> has an additional option parameter
                    <em class="emphasis">nestSeparator</em>, which defines with which character the
                    single nodes are separated. The default is a single dot, which is accepted by
                    <span class="classname">Zend_Config_Ini</span> by default.
                </p>
            </li>
        </ul>
    </div>

    <div class="section" id="zend.config.writer.introduction.yaml-notes"><div class="info"><h1 class="title">Notes specific to the YAML writer</h1></div>
        

        <p class="para">
            The <acronym class="acronym">YAML</acronym> writer lets you optionally specify an alternate
            <acronym class="acronym">YAML</acronym> encoder to use. By default, one is shipped with the framework
            that is suitable for most configuration tasks. If you find it insufficient, or wish to
            use more advanced YAML, you may provide an alternate encoder callback.
        </p>

        <p class="para">
            The method for doing so is to use the
             <span class="methodname">Zend_Config_Writer_Yaml::setYamlEncoder()</span> method, passing it a
            valid callback.
        </p>

        <pre class="programlisting brush: php">
// Use the Symfony Yaml Component:
$writer = new Zend_Config_Writer_Yaml($filename);
$writer-&gt;setYamlEncoder(array(&#039;sfYaml&#039;, &#039;dump&#039;));
</pre>


        <p class="para">
            The above uses the Symfony Components&#039; <span class="classname">sfYaml</span> component in order
            to encode the configuration to <acronym class="acronym">YAML</acronym>.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.config.writer.html">Zend_Config_Writer</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.config.writer.html">Zend_Config_Writer</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.console.getopt.html">Zend_Console_Getopt</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.config.writer.html">Zend_Config_Writer</a></li>
  <li class="active"><a href="zend.config.writer.introduction.html">Zend_Config_Writer</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>